package com.wwwarehouse.contract.contract_address;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.adapter.ChooseAddressAdapter;
import com.wwwarehouse.contract.bean.AddressListBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.ChooseAddressEvent;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/10/19.
 * 选择地址列表 listview
 */

public class ChooseAddressListViewFragment extends BaseFragment {
	private ListView mListView;
	private List<AddressListBean.ListBean> list = new ArrayList<>();
	private ChooseAddressAdapter chooseAddressAdapter;
	private int mHeight;
	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.choose_address_listview_layout , container , false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mListView = (ListView) view.findViewById(R.id.choose_lv);
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				chooseAddressAdapter.setSelectPosition(position);
				chooseAddressAdapter.notifyDataSetChanged();
				popFragment();
				EventBus.getDefault().post(new ChooseAddressEvent(list.get(position)));
			}
		});
	}

	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		if (enter) {
			if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA) != null) {
				final AddressListBean listBean = (AddressListBean) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);
				String mAddressUkid = null;
				if (listBean != null) {
					 mAddressUkid = listBean.getmAddressUkid();
				}
				int position = getArguments().getInt(ContractConstant.KEY_BUNDLE_POSITION);
				int pageNum = getArguments().getInt(ContractConstant.KEY_BUNDLE_PAGENUM);
				list.clear();
				for (int i = position * pageNum; i < (position + 1) * pageNum; i++) {
					if (listBean != null) {
						if (i >= 0 && i < listBean.getList().size()) {
							if (StringUtils.isNoneNullString(mAddressUkid)) {
								if (mAddressUkid.equals(listBean.getList().get(i).getAddressCommonUkid())) {
									//上次选择的
									listBean.getList().get(i).setSelected(true);
								}

							}
							list.add(listBean.getList().get(i));
						}
					}

				}
				mListView.post(new Runnable() {
					@Override
					public void run() {
						chooseAddressAdapter = new ChooseAddressAdapter(list, mActivity , mListView.getHeight() - mListView.getPaddingBottom() - 20);
						mListView.setAdapter(chooseAddressAdapter);
					}
				});

//
			}
		}
		return null;
	}


	@Override
	public void setTitle() {

	}

	@Override
	public void requestDatas() {




	}


}
